package com.atguigu.springdemo.controller;

import com.atguigu.springdemo.beans.Employee;
import com.atguigu.springdemo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.invoke.SwitchPoint;

@RestController
public class EmployeeController {
    //controller层得调Service业务逻辑,因此提供service属性
    @Autowired//自动感知你是什么类型,就去容器中找什么类型,然后给你赋值
    private EmployeeService service;

    //提供方法处理请求
    @RequestMapping(value = "/emp")//接收请求参数
    public Object handle1(Integer id, String lastName, String gender, String email, String op) {
        Employee emp = new Employee(id, lastName, gender, email);

        switch (op) {
            case "select":
                //调用service层业务逻辑,因此声明对业务做增删改查的业务模型类
                Employee employee = service.getEmployee(id);
                //如果id是非法的,employee会使null,进行判断
                if (employee == null) {
                    return "id非法";
                }
                return employee;
            case "insert":
                service.insertEmployee(emp);
                return "success";
            case "update":
                service.updateEmployee(emp);
                return "success";
            case "delete":
                service.deleteEmployee(id);
                return "success";
            default:return "success";

        }
    }


    @RequestMapping(value = "/getall")
    public Object handle2() {
        return service.selectAll();
    }


}
